package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.srwl.mytx.utils.DateUtils;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.MultipleCouponDetailActivity;
import com.srwl.mytx.activity.SingleCouponDetailActivity;
import com.srwl.mytx.domain.Coupon;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;

import java.util.List;

/**
 * Created by max on 14/12/11
 */
public class CouponReceivedRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private boolean bol_noData;//标记是否有数据
    private boolean bol_requestError;//标记是否有数据


    private Context mContext;
    private List<Coupon> couponList;


    public CouponReceivedRecordAdapter(Context context, List<Coupon> couponList) {
        mContext = context;
        this.couponList = couponList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.no__data__layout, parent, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.request_error_layout, parent, false));
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rp_received_record_list_item, parent, false);
        return new ItemViewHolder(view);


    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有记录");
        } else if (viewType == Constant.ITEM_TYPE_NORMAL) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            setItemViews(itemViewHolder, position);
        }
    }


    private void setItemViews(ItemViewHolder itemViewHolder, int position) {
        Coupon coupon = couponList.get(position);
        User sendUser = coupon.getTargetUser();
        itemViewHolder.tvDate.setText(DateUtils.getTimestampString(coupon.getCreated()));
        itemViewHolder.tvSenderName.setText(sendUser.getUsername());

        itemViewHolder.tvMoney.setText(Utils.moneyFormat(coupon.getAmount()));
        if (coupon.getType() == Constant.COUPON_TYPE_ALI_FUND) {
            itemViewHolder.tvType.setText("支付宝红包");
            itemViewHolder.iv_couponType.setVisibility(View.GONE);
        } else {
            if (coupon.getType() == Constant.COUPON_TYPE_CHILD_RANDOM) {
                itemViewHolder.iv_couponType.setBackgroundResource(R.drawable.rp_random_icon);
                itemViewHolder.iv_couponType.setVisibility(View.VISIBLE);
                itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mContext.startActivity(new Intent(mContext, MultipleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_CHILD_COUPON, coupon));
                    }
                });

            } else if (coupon.getType() == Constant.COUPON_TYPE_CHILD_AVERAGE) {
                itemViewHolder.iv_couponType.setVisibility(View.GONE);
                itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mContext.startActivity(new Intent(mContext, MultipleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_CHILD_COUPON, coupon));
                    }
                });
            } else {
                itemViewHolder.iv_couponType.setVisibility(View.GONE);
                itemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mContext.startActivity(new Intent(mContext, SingleCouponDetailActivity.class).putExtra(Constant.EXTRA_INFO_COUPON, coupon));
                    }
                });
            }
            itemViewHolder.tvType.setText("金币红包");
        }

//        Glide.with(mContext).load(sendUser.getAvatar())
//                .placeholder(R.drawable.tx_default_avatar)
//                .into(itemViewHolder.ivAvatar);

        GlideLoadUtils.glideLoad(mContext, sendUser.getAvatar(), itemViewHolder.ivAvatar, R.drawable.tx_default_avatar_1);

    }

    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return couponList.size();
    }


    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvSenderName;
        TextView tvMoney;
        TextView tvDate;

        ImageView ivAvatar;
        TextView tvType;
        ImageView iv_couponType;

        ItemViewHolder(View v) {
            super(v);
            initView(v);
        }

        private void initView(View v) {
            tvSenderName = v.findViewById(R.id.tv_sender_nickname);
            tvMoney = v.findViewById(R.id.tv_item_money_amount);
            tvDate = v.findViewById(R.id.tv_time);
            ivAvatar = v.findViewById(R.id.iv_record_sender_avatar);
            tvType = v.findViewById(R.id.tv_item_type);

            iv_couponType = v.findViewById(R.id.iv_coupon_type);
        }
    }


}
